﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class SinhVien_TichLuyKhoiRepository : ISinhVien_TichLuyKhoiRepository
    {
        DangKyPhuEntities entities;
        public SinhVien_TichLuyKhoiRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region ISinhVien_TichLuyKhoiRepository Members

        public IQueryable<Entities.SinhVien_TichLuyKhoi> LayDsSVTLTheoKhoi(int khoiID, int hocKyID)
        {
            return from m in entities.SinhVien_TichLuyKhoi
                   where m.KhoiCTDTID == khoiID
                   && m.HocKyTinh == hocKyID
                   select m;
        }

        #endregion

        public IQueryable<Entities.SinhVien_TichLuyKhoi> LayDsSVTLTheoHocKy(int hocKyID)
        {
            return from m in entities.SinhVien_TichLuyKhoi
                   where m.HocKyTinh == hocKyID
                   select m;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MSSV"></param>
        /// <param name="khoiID"></param>
        /// <param name="dtbKhoi"></param>
        /// <param name="hocKy"></param>
        /// <param name="dtbtlChung"></param>
        public void ThemSinhVienTLTheoKhoi(string MSSV, int khoiID,float dtbKhoi,int hocKy,float dtbtlChung, int rot)
        {
            SinhVien_TichLuyKhoi result = new SinhVien_TichLuyKhoi()
            {
                MSSV = MSSV,
                KhoiCTDTID = khoiID,
                HocKyTinh = hocKy,
                DTBTichLuyTheoSVXet = dtbtlChung,
                DTBTichLuyTheoKhoi = dtbKhoi,
                Rot =rot
            };
            entities.AddToSinhVien_TichLuyKhoi(result);
            entities.SaveChanges();
        }

        public bool KiemTraTonTai(string MSSV, int KhoiID, int HocKy)
        {
            var result = from m in entities.SinhVien_TichLuyKhoi
                         where m.MSSV.Equals(MSSV)
                         && m.KhoiCTDTID == KhoiID
                         && m.HocKyTinh == HocKy
                         select m;
            if (result.Count() > 0)
                return true;
            return false;
        }


        public void CapNhatDTBKhoiVaRotSinhVienTLKhoi(string MSSV, int khoiID, float dtbKhoi, int hocKy, int rot)
        {
            var result = (from m in entities.SinhVien_TichLuyKhoi
                         where m.MSSV.Equals(MSSV)
                         && m.KhoiCTDTID == khoiID
                         && m.HocKyTinh == hocKy
                         select m).FirstOrDefault();
            result.DTBTichLuyTheoKhoi = dtbKhoi;
            result.Rot = rot;
            entities.SaveChanges();
        }
    }
}
